<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增应用详细内容')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-appsys-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">序列号：</label>
                <div class="col-sm-8">
                    <input name="serialNum" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">功能logo：</label>
                <div class="col-sm-8">
                    <input id="logoImg" name="logoImg" class="form-control" type="text" readonly>
                    <div class="photo-add">
                        <input  type="file" accept="image/*"
                               name="uploader-input"
                               class="uploader-file"
                               id="upload">
                    </div>
                    <div id="box"></div>

                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">功能名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">网址：</label>
                <div class="col-sm-8">
                    <textarea name="url" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">一句话介绍：</label>
                <div class="col-sm-8">
                    <input name="descrip" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">分类：</label>
                <div class="col-sm-8">
                    <select name="categoryId">
                        <option value="0">生活</option>
                        <option value="1">资讯</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select name="type" class="form-control m-b">
                        <option value="0">自建</option>
                        <option value="1">第三方</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">是否默认订阅：0是 1否：</label>
                <div class="col-sm-8">
                    <select name="subscribe">
                        <option value="">所有</option>
                        <option value="0">是</option>
                        <option value="1">否</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box">
                        <select name="status">
                            <option value="">所有</option>
                            <option value="0">启用</option>
                            <option value="1">停用</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">应用内容：</label>
                <div class="col-sm-8">
                    <textarea name="introduct" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">介绍应用内容正文的图片：</label>
                <div class="col-sm-8">
                    <textarea name="introductImg" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">应用详情：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" name="text">
                    <div class="summernote" id="text"></div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/appsys"
        $("#form-appsys-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-appsys-add').serialize());
            }
        }

        $("input[name='addtime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
        });

        document.getElementById('upload').addEventListener('change', function (event) {
            var $file = event.currentTarget;
            var formData = new FormData();
            var file = $file.files;
            formData = new FormData();
            formData.append("file", file[0]);
            $.ajax({
                url: ctx + "common/upload",
                type: 'POST',
                dataType: 'json',
                data: formData,
                contentType: false,
                processData: false
            })
                .done(data => {
                    console.log(data);
                    $('#logoImg').val(data.url);
                    $('#box').append(`<div class="photo-item">
            <img class="item-image" width="100%" height="100%" src="${data.url}"/>
        </div>`);
                })
                .fail(data => {
                    console.log(data);
                });
        });

    </script>


</body>
</html>
